package org.java.dao.DaoImpl;

import org.java.dao.ArtUserDao;
import org.java.util.JDBCUtil;
import org.springframework.jdbc.core.JdbcTemplate;

public class UserDaoImpl implements ArtUserDao {
    private JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtil.getDataSource());

    //登录  查询数据库是否有此用户
    @Override
    public boolean sign(String name,String pwd) {
        String sql="select count(*) from user where UserName=? and UserPwd=?";
        int row=jdbcTemplate.queryForObject(sql,Integer.class,name,pwd);
        return row>0;
    }

    @Override
    public boolean sense(String name) {

        String sql="select count(*) from user where UserName=?";
        int row=jdbcTemplate.queryForObject(sql,Integer.class,name);
        return row>0;
    }

    @Override
    public void userAdd(String name, String pwd,String email,String phone) {
        String sql="insert into user values(uuid(),?,?,?,?)";
        jdbcTemplate.update(sql,name,pwd,email,phone);
    }

    @Override
    public boolean judegEmial(String mail) {

        String sql="select count(*) from user where UserEmail=?";
        Integer row = jdbcTemplate.queryForObject(sql, Integer.class, mail);
        System.out.println(row);
        return row>0;
    }

    @Override
    public boolean forget(String name, String mail) {
        String sql="select count(*) from user where UserName=? and UserEmail=?";
        int row=jdbcTemplate.queryForObject(sql,Integer.class,name,mail);
        return row>0;
    }

    @Override
    public void reSetPwd(String name,String pwd) {
        String sql="update user set UserPwd=? where UserName=?";
        jdbcTemplate.update(sql,pwd,name);
    }

    @Override
    public String getUserId(String name, String pwd) {
        String sql="select UserId from user where UserName=? and UserPwd=?";
        return jdbcTemplate.queryForObject(sql,String.class,name,pwd);
    }
}
